#!/bin/bash

git reset --hard HEAD

# 获取当前公网 IP
current_ip=$(curl -s https://icanhazip.com/)

# 检查是否成功获取到 IP
if [ -z "$current_ip" ]; then
    echo "无法获取公网 IP，脚本退出。"
    exit 1
fi

# 读取 ip.txt 文件中的 IP
if [ -f "ip.txt" ]; then
    old_ip=$(cat ip.txt)
else
    old_ip=""
fi

# 比较 IP 是否一致
if [ "$current_ip" = "$old_ip" ]; then
    echo "公网 IP 未改变，脚本退出。"
    exit 0
fi

# 将新 IP 写入 ip.txt
echo "$current_ip\t\t1742l0b008.goho.co" > ip.txt

if [ $? -ne 0 ]; then
    echo "克隆 Git 仓库失败，脚本退出。"
    exit 1
fi


# 添加、提交并推送更改
git add ip.txt
git commit -m "更新公网 IP 到 $current_ip"
git push
if [ $? -eq 0 ]; then
    echo "公网 IP 已更新并成功推送到 Git 仓库。"
else
    echo "推送更改到 Git 仓库失败。"
fi
